/**
 * 
 */
package com.java.test.messagecenter.mapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.messagecenter.dto.AppUpdateInfo;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Component
public class AppUpdateMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectappupdateCount(AppUpdateInfo info) {

		String sql = "select count(*) from terminal_appupdate where 1=1 ";

		if (info.getUpdateMode() != null && !"".equals(info.getUpdateMode())) {
			sql += " and update_mode='" + info.getUpdateMode() + "'";
		}

		if (info.getTerminalName() != null && !"".equals(info.getTerminalName())) {
			sql += " and terminal_name ='" + info.getTerminalName() + "'";
		}

		if (info.getCreateTime() != null) {
			sql += " and TO_DAYS(create_time)= TO_DAYS('" + info.getCreateTime() + "')";
		}

		int i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i;
	}

	public List<AppUpdateInfo> selectappupdateList(AppUpdateInfo info, PageInfo<AppUpdateInfo> page) {

		String sql = "select * from terminal_appupdate where 1=1 ";

		if (info.getUpdateMode() != null && !"".equals(info.getUpdateMode())) {
			sql += " and update_mode='" + info.getUpdateMode() + "'";
		}

		if (info.getTerminalName() != null && !"".equals(info.getTerminalName())) {
			sql += " and terminal_name ='" + info.getTerminalName() + "'";
		}

		if (info.getCreateTime() != null) {
			sql += " and TO_DAYS(create_time)= TO_DAYS('" + info.getCreateTime() + "')";
		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " limit ?,?";

		List<AppUpdateInfo> list = jdbcTemplate.query(sql, new AppUpdateInfo(), start, limit);

		return list;
	}

	public AppUpdateInfo selectappupdateInfo(AppUpdateInfo info) {
		String sql = "select * from terminal_appupdate where id=?";

		AppUpdateInfo infos = jdbcTemplate.queryForObject(sql, new AppUpdateInfo(), info.getId());

		return infos;
	}

	public int updateappupdateInfo(AppUpdateInfo info) {

		String sql = "update terminal_appupdate set update_address=?,content=?,channel_id=?,update_mode=?,version_number=?,terminal_name=? where id=?";

		int i = jdbcTemplate.update(sql, info.getUpdateAddress(), info.getContent(), info.getChannelId(),
				info.getUpdateMode(), info.getVersionNumber(),info.getTerminalName(), info.getId());

		return i;
	}

	public int insertappupdateInfo(AppUpdateInfo info) {

		String sql = "insert into terminal_appupdate(terminal_name,channel_id,update_mode,version_number,update_address,content,add_people,create_time)"
				+ "values(?,?,?,?,?,?,?,?)";
		int i = jdbcTemplate.update(sql, info.getTerminalName(),info.getChannelId(), info.getUpdateMode(), info.getVersionNumber(),
				info.getUpdateAddress(), info.getContent(), info.getAddPeople(), info.getCreateTime());

		return i;

	}

	/**
	 * @param info
	 * @return
	 */
	public int deleteappupdateInfo(AppUpdateInfo info) {

		String sql = "delete from  terminal_appupdate where id=?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;
	}

}
